package com.mcms.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.mcms.domain.User;
import com.mcms.service.UserService;

public class LoginController extends AbstractCommandController {
	private UserService userService;

	public LoginController() {
		setCommandClass(LoginCommand.class);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		/**参数获取*/
		String actionType = request.getParameter("actionType");// 控制跳转参数
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		System.out.println("at:"+actionType+";un:"+userName+";pw:"+password+";code:"+code);
		
		if(actionType==null || actionType.trim().length()==0){
			System.out.println("----------");
			return new ModelAndView("login");
		}else{
			actionType = actionType.trim();
		}
		
		
		
		/**登陆页跳转*/
		if(actionType.equals("loginPage")){
			return new ModelAndView("login");
		}
		
		/**登陆验证处理*/
		if(actionType.equals("loginController")){
//			LoginCommand loginCommand = (LoginCommand) command;
//			boolean isValidUser = userService.hasMatchUser(loginCommand
//					.getUserName(), loginCommand.getPassword());
//			if (!isValidUser) {
//				return new ModelAndView("login", "error", "用户名或密码错误。");
//			} else {
//				User user = userService.findUserByUserName(loginCommand
//						.getUserName());
//				user.setLastIp(request.getLocalAddr());
//				user.setLastVisit(new Date());
//				userService.loginSuccess(user);
//				request.getSession().setAttribute("user", user);
//				return new ModelAndView("index");
//			}
			return new ModelAndView("index");
		}
		
		/**默认跳转至登陆页面*/
		return new ModelAndView("login");

	}
}
